<!doctype html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>修改用户信息</title>
</head>
<body>

<form action="msg.php" method="post" name="fname">
    <table width="50%" border="0" cellpadding="0" cellspacing="2">
        <tr>
            <td width="24%" height="29">用户名：</td>
            <td><input type="text" name="user_name" id="user_name" size="20" pattern="[A-Za-z\-\u4e00-\u9fa5]{2,6}"
                       title="格式错误">
            </td>
        </tr>
        <tr>
            <td>来自城市：</td>
            <td><select name="city">
                    <option value="Beijing" selected>北京</option>
                    <option value="NewYork">纽约</option>
                    <option value="Paris">巴黎</option>
                    <option value="London">伦敦</option>
                    <option value="Rome">罗马</option>
                </select></td>
        </tr>
        <tr>
            <td>注册时间：</td>
            <td><?php echo @date("Y-m-d"); ?></td>
        </tr>
        <tr>
            <td>
                <button type="submit" name="submit" id="btn">修改</button>

            </td>
        </tr>


    </table>
</form>

<script>
    function login() {
        var uName = document.forms["fname"]["user_name"],
            btn = document.forms["fname"]["submit"];
        btn.onclick = function () {
            if (uName.value === "") {
                alert("用户名不能为空");
                return false;
            } else {
                return true;
            }
        };
    }

    login();
</script>
</body>
</html>

<?php
/**
 * Created by PhpStorm.
 * User: Office
 * Date: 2017/12/28
 * Time: 14:07
 */


if(isset($_POST["submit"])){
    xiu();
}
function xiu(){
    header("content-type:text/html;charset=utf-8");
    $conn = mysqli_connect("localhost", "root", "root", "test");
    if (!$conn) {
        exit("连接失败 " . mysqli_error($conn));
    }
    if (!isset($_POST['user_name'])) {
        echo '参数错误！';
        exit;
    }

    $arr_city = array('Beijing' => '北京', 'NewYork' => '纽约', 'Paris' => '巴黎', 'London' => '伦敦', 'Rome' => '罗马');

    $id = $_POST['user_name'];
    $sql = "select * from user WHERE username = '{$id}'";
    $result = mysqli_query($conn, $sql) or die("<br/>ERROR:<b>" . mysqli_error() . "</b><br/>SQL:" . $sql);

    if (!mysqli_num_rows($result)) {
        die("用户ID错误");
    }
    $row = mysqli_fetch_array($result);

    $name = $_POST['user_name'];
    $city = $_POST['city'];


    if (!empty($name) || trim($name) !== "") {
        $sql = "update user set username = '{$name}',city = '{$city} ' WHERE  username = '{$name}'";
        mysqli_query($conn, $sql) or exit("<br/>ERROR:,<b>" . mysqli_error() . "</b><br/>SQL:" . $sql);
        mysqli_close($conn);
        echo '数据修改成功,打开<a href="php/pagingData.php">pagingData.php</a>查看数据';
        exit;
    }
}

?>